/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package shattered2;

import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;

/**
 *
 * @author 10135228
 */
public class shattered2 {

    static final int amount = 15900;
    final static BufferedImage[] pics = new BufferedImage[amount];
//    
//    HashMap<String, ArrayList<PuzzlePiece>> topCodeTable = new HashMap<>();
//    HashMap<String, ArrayList<PuzzlePiece>> leftCodeTable = new HashMap<>();
//    HashMap<String, ArrayList<PuzzlePiece>> bottomCodeTable = new HashMap<>();
//    HashMap<String, ArrayList<PuzzlePiece>> rightCodeTable = new HashMap<>();
    PuzzlePiece[] pieces = new PuzzlePiece[amount];
    //@SuppressWarnings({"rawtypes","unchecked"})
    //HashMap<String, ArrayList<PuzzlePiece>>[] CodeTables = new HashMap[]{topCodeTable,leftCodeTable,bottomCodeTable,rightCodeTable};
    
    public shattered2() {
        getImages();
        for (int i = 0; i < amount; i++) {
            PuzzlePiece tmp = new PuzzlePiece(i);
            pieces[i] = tmp;
//            if(topCodeTable.get(tmp.getTopCode())==null){
//                topCodeTable.put(tmp.getTopCode(), new ArrayList<PuzzlePiece>());
//            }
//            topCodeTable.get(tmp.getTopCode()).add(tmp);
//            
//            if(leftCodeTable.get(tmp.getLeftCode())==null){
//                leftCodeTable.put(tmp.getLeftCode(), new ArrayList<PuzzlePiece>());
//            }
//            leftCodeTable.get(tmp.getLeftCode()).add(tmp);
//            
//            if(bottomCodeTable.get(tmp.getBottomCode())==null){
//                bottomCodeTable.put(tmp.getBottomCode(), new ArrayList<PuzzlePiece>());
//            }
//            bottomCodeTable.get(tmp.getBottomCode()).add(tmp);
//            
//            if(rightCodeTable.get(tmp.getRightCode())==null){
//                rightCodeTable.put(tmp.getRightCode(), new ArrayList<PuzzlePiece>());
//            }
//            rightCodeTable.get(tmp.getRightCode()).add(tmp);                    
        }
        new MainFrame(pieces);

    }
    
    private BufferedImage IconToImage(Object o){
        ImageIcon icon = (ImageIcon)o;
        BufferedImage img = new BufferedImage(icon.getIconWidth(),icon.getIconHeight(),BufferedImage.TYPE_INT_ARGB);
        img.getGraphics().drawImage(icon.getImage(), 0, 0, null);
        return img;
    }

    private void getImages() {
        System.out.println("Getting images");
        Socket mySocket = null;
        BufferedWriter out = null;
        ObjectInputStream in = null;
        
        try {
            mySocket = new Socket("localhost", 22222);
            out = new BufferedWriter(new PrintWriter(mySocket.getOutputStream()));
            in = new ObjectInputStream(mySocket.getInputStream());
            out.write(Integer.toString(amount));
            out.newLine();
            out.flush();
            long timeStart = System.currentTimeMillis();
            for (int i = 0; i < amount; i++) {
                pics[i] = IconToImage(in.readObject()); 
            }
            long timeEnd = System.currentTimeMillis();
            String format = String.format("%%0%dd", 2);  
            long elapsedTime = (timeEnd-timeStart) / 1000;  
            String seconds = String.format(format, elapsedTime % 60);  
            String minutes = String.format(format, (elapsedTime % 3600) / 60);  
            String hours = String.format(format, elapsedTime / 3600);  
            String time =  hours + ":" + minutes + ":" + seconds; 
            Toolkit.getDefaultToolkit().beep();
            System.out.println("Done getting images in "+time);
        }catch(ConnectException ex){ 
            System.err.println("Server not running!");
        } catch (UnknownHostException ex) {
            Logger.getLogger(shattered2.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException | ClassNotFoundException ex) {
            Logger.getLogger(shattered2.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                in.close();
                out.close();
            } catch (IOException ex) {
                Logger.getLogger(shattered2.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    

    
}
